package com.Sony.TemplateGame;

import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.FixedStepPhysicsWorld;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;


/**
 *	Class untuk mengatur semua item pada setiap scene
 *	@author sony wirawan
 **/
public class GameItem 
{
	private TemplateGameActivity core;
	private ObjectUserData item_userdata;
	
	private BitmapTextureAtlas mTextureItem;
	private TextureRegion mItemTextureRegion;
	public Sprite item;
	public Body mItemBody;
	
	
	/**
	 *	Constructor item
	 *	@param BA = context yang digunakan
	 *	@author sony wirawan
	 **/
	public GameItem(TemplateGameActivity template)
	{	// TODO Auto-generated constructor stub
		this.core = template;
	}
	
	
	/**
	 *	Method untuk membuat item
	 *	@param mPhysicsWorld, implementasi dari physics library yang digunakan
	 *	@param name, path file untuk grafis item
	 *	@param code, nama item
	 *	@return body dari item
	 *	@author sony wirawan
	 **/
	public Body createItem
	(	FixedStepPhysicsWorld mPhysicsWorld,
		String name,
		String code
	)
	{	// TODO Auto-generated method stub
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		
		// item texture
		this.mTextureItem = new BitmapTextureAtlas(128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mItemTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mTextureItem, this.core, name, 0, 0);
		
		// Load the textures
		this.core.getEngine().getTextureManager().loadTextures(this.mTextureItem, this.mTextureItem);
		
		// create item sprite
		this.item = new Sprite(0, 0, 32, 32, this.mItemTextureRegion);
		
		// create body item
		final FixtureDef itemFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0.5f);
		this.mItemBody = PhysicsFactory.createBoxBody(mPhysicsWorld, this.item, BodyType.StaticBody, itemFixtureDef);
		mPhysicsWorld.registerPhysicsConnector
		(	new PhysicsConnector( this.item, this.mItemBody ));
			
		// set item attribute
		this.item_userdata = new ObjectUserData
		( code, this.item );
		this.mItemBody.setUserData(this.item_userdata);
		this.mItemBody.setActive(false);
		this.item.setVisible(false);
		
		return this.mItemBody;
	}
}
